<?php

class CheckimeiController extends Controller
{
	
	//public $layout='//layouts/column2';
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('view','lrsuccessfull'),
				'users'=>array('*'),
			),

			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('checkimei', 'history'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('index','admin','delete','SuggestPerson'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}
	
	public function actionIndex()
	{
		
		$criteria=new CDbCriteria;
		$criteria->order = 'ngaycheck DESC';
		$Page = new CPagination(Checkimei::model()->count($criteria));
		$Page->pageSize = 99999999999;
		$Page->applyLimit($criteria);
		$data['checkimeis'] =  Checkimei::model()->findAll($criteria);
		$data['pages'] = $Page;
		$this->render('index', $data);
	}
	
	public function actionSuggestPerson() {
		$criteria = new CDbCriteria;
		$criteria->select = array('id', 'username');
	
		$criteria->addSearchCondition('username',  strtoupper( $_POST['term']) ) ;
		$criteria->limit = 15;
		$data = User::model()->findAll($criteria);
	
		$arr = array();
	
		foreach ($data as $item) {
			 
			$arr[] = array(
					'id'=>$item->id,
					'username' => $item->username,
					'hoten' => $item->hoten,
					'email' => $item->email,
					'dienthoai' => $item->dienthoai,
					'trangthai' => $item->trangthai
					
			);
		}
	
		echo CJSON::encode($arr);
	
	}
	
	public function actionHistory(){
	/*	$criteria=new CDbCriteria;
		$criteria->order = 'ngaycheck DESC';
		$criteria->compare('iduser', UserUtils::getUserId());
		$Page = new CPagination(Checkimei::model()->count($criteria));
		$Page->pageSize = 50;
		$Page->applyLimit($criteria);
		$data['checkimeis'] =  Checkimei::model()->findAll($criteria);
		$data['pages'] = $Page;
		*/
		$data['model'] = new Checkimei('search');
		if (isset($_REQUEST['Checkimei'])) {
		    $data['model']->attributes = $_REQUEST['Checkimei'];
		}
		$data['model']->iduser = UserUtils::getUserId();
		$this->render('history', $data);
	}
}